var CENTERTABS;		//TAB面板
var BUSSINESS_WINDOW;
var LOADING_MESSAGE = '正在处理，请稍候。。。';
$(function() {	

});

/**
 * 初始化TAB面板
 * @param TABPANEL_ID	TAB面板ID
 */
function CENTERTAB_INIT(TABPANEL_ID){
	CENTERTABS = TABPANEL_ID.tabs({
					border:false
				});
}

/**
 * 初始化首页
 */
function MAIN_INIT(){
	//MESSAGEBOX('首页');
	CENTERTABS.tabs('add',{
		title:'首页',
		href:'main!show'
		//content : '<iframe src="main!show" frameborder="0" style="border:0;width:100%; height:100%;"></iframe>',
	});
}

/**
 * 获取组件编号
 * @param SUBASSEMBLY_NAME	组件名称
 * @returns
 */
function GETID(SUBASSEMBLY_NAME){
	return $('#' + SUBASSEMBLY_NAME + '');
}

/**
 * 判断是否选中记录
 * @param row
 */
function ROW_IS_NOT_NULL(ROW){
	if(ROW == null){
		MESSAGEBOX('请先选择一条记录!');
		return false;
	}else{
		return true;
	}
}

/**
 * 判断对象为空则返回空
 * @param PARAM
 * @returns
 */
function OBJ_IS_EMPTY(PARAM){
	if(PARAM == undefined){
		return '';
	}else{
		return PARAM;
	}
}

/**
 * 初始化面板
 * @param PANEL_ID
 */
function PANEL_INIT(PANEL_ID){
	PANEL_ID.panel({
		height: 31,
		border:false
	});
}

/**
 * 初始化窗口
 * @param WINDOW_ID	窗口ID
 * @param WIDTH		宽度
 * @param HEIGHT	高度
 */
function WINDOW_INIT(WINDOW_ID, TITLE, WIDTH, HEIGHT, ICON){
	WINDOW_ID.window({
			title:TITLE,
			closed:true,
			modal:true,
			width:WIDTH,
			height:HEIGHT,
			iconCls:ICON,
			closable:true,
			resizable:false,
			minimizable:false,
			maximizable:false,
			collapsible:false,
			loadingMessage:LOADING_MESSAGE
	});
}

/**
 * 初始化业务窗口
 * @param WINDOW_ID
 * @param WIDTH
 * @param HEIGHT
 */
function BS_WINDOW_INIT(WINDOW_ID, WIDTH, HEIGHT){
	WINDOW_ID.window({
			//title:TITLE,
			closed:true,
			modal:true,
			width:WIDTH,
			height:HEIGHT,
			//iconCls:ICON,
			closable:true,
			resizable:false,
			minimizable:false,
			maximizable:false,
			collapsible:false,
			loadingMessage:LOADING_MESSAGE
	});
}

/**
 * 初始化业务窗口
 * @param WINDOW_ID
 * @param WIDTH
 * @param HEIGHT
 */
function WINDOW_BUSINESS_INIT(WINDOW_ID, WIDTH, HEIGHT){
	BUSSINESS_WINDOW = WINDOW_ID;
	WINDOW_ID.window({
		//href:URL,
		closed:true,
		modal:true,
		width:WIDTH,
		height:HEIGHT,
		iconCls:'icon-view',
		closable:true,
		resizable:true,
		minimizable:false,
		maximizable:false,
		collapsible:true,
		loadingMessage:LOADING_MESSAGE
	});
}

/**
 * 打开窗口
 * @param WINDOW_ID	窗口ID
 */
function WINDOW_OPEN(WINDOW_ID){
	WINDOW_ID.window('open');
}

/**
 * 打开业务窗口
 * @param WINDOW_ID	窗口ID
 * @param TITLE	标题
 * @param ICON	图标
 */
function BS_WINDOW_OPEN(WINDOW_ID, TITLE, ICON){
	ICON = ICONS(ICON);
	WINDOW_ID.window({title:TITLE});
	WINDOW_ID.window({iconCls:ICON});
	WINDOW_ID.window('open');
}

/**
 * 关闭窗口
 * @param WINDOW_ID	窗口ID
 */
function WINDOW_CLOSE(WINDOW_ID){
	WINDOW_ID.window('close');
}

/**
 * 关闭业务窗口
 */
function CLOSE_WF_BUS_PAGE(){
	WINDOW_CLOSE(BUSSINESS_WINDOW);
}


function ICONS(ICON){
	if(ICON == 'add'){
		return 'icon-add';
	}else if(ICON == 'edit'){
		return 'icon-edit';
	}else if(ICON == 'view'){
		return 'icon-view';
	}else if(ICON == 'search'){
		return 'icon-search';
	}else if(ICON == 'task'){
		return 'icon-task';
	}
}

/**
 * 打开业务窗口
 * @param WINDOW_ID
 * @param TITLE
 * @param URL
 */
function WINDOW_BUSINESS_OPEN(WINDOW_ID, TITLE, URL){
	WINDOW_ID.window('setTitle', TITLE);
	WINDOW_OPEN(WINDOW_ID, '浏览', 'view');
	WINDOW_ID.window('refresh', URL);
}

/**
 * 初始化对话框
 * @param DIALOG_ID		对话框ID
 * @param TITLE			标题
 * @param WIDTH			宽度
 * @param HEIGHT		高度
 * @param SAVEFUNC		保存方法
 */
function DIALOG_INIT(DIALOG_ID, TITLE, WIDTH, HEIGHT, SAVEFUNC){
	DIALOG_ID.dialog({
		title:TITLE,
		width:WIDTH,
		height:HEIGHT,
		//icons:'icon-view',
		buttons:[{
			text:'保存',
			iconCls:'icon-save',
			handler:function(){
				SAVEFUNC();
			}
		},{
			text:'关闭',
			iconCls:'icon-close',
			handler:function(){
				DIALOG_ID.dialog('close');
			}
		}]
	});
}

/**
 * 信息提示
 * @param MESSAGE	信息内容
 */
function MESSAGEBOX(MESSAGE){
	//$.messager.alert('提示', MESSAGE);
	$.messager.show({
		title:'提示',
		msg:MESSAGE,
		timeout:2000,
		showType:'fade'
	});
}

function DG_WF_INIT(DATAGRID_ID, URL, COLUMNS, PAGE, SINGLE){
	//初始化数据表格
	DATAGRID_ID.datagrid({
		url:URL,
		idField:'id',
		sortName: 'id',
		sortOrder: 'desc',
		loadMsg:LOADING_MESSAGE,
		border:false,
		nowrap: false,
		striped: true,
		remoteSort:false,
		rownumbers:true,
		pagination:PAGE,
		collapsible:true,
		singleSelect:SINGLE,
		columns:[COLUMNS],
		onDblClickRow: function(rowIndex, rowData){
			//var URL = 'borrowMoney!view?id=' + rowData.idValue; 
			var URL = rowData.url;
			URL = URL.replace('{id}', rowData.idValue);
			$.post('doAutAjax!addBusinessWorkflowParam', {idValue: rowData.idValue, modelName:rowData.modelName, modelIdName: rowData.modelIdName, processId: rowData.processId, dgName: 'affairsBeDealtWithDataGrid'},
					function(data){
						WINDOW_BUSINESS_OPEN(BUSSINESS_WINDOW, '浏览', URL);
						WF_SET_CLOSEFUN(CLOSE_WF_BUS_PAGE);
			}, 'json');
		}
	});
}

/**
 * 初始化数据表格
 * @param DATAGRID_ID	设置数据表格ID
 * @param URL			设置数据请求地址
 * @param COLUMNS		设置显示字段
 * @param PAGE			设置是否分页
 * @param SINGLE		设置是否单选
 * @param TOOLBAR		设置工具栏
 * @param BUSINESSNAME	设置业务名称
 */
function DG_INIT(DATAGRID_ID, URL, COLUMNS, PAGE, SINGLE, TOOLBAR, BUSINESSNAME){
	//如果工具栏 = null 则不设置工具栏
	if(TOOLBAR == null){
		TOOLBAR = '';
	}
	
	//初始化数据表格
	DATAGRID_ID.datagrid({
		url:URL,
		idField:'id',
		sortName: 'id',
		sortOrder: 'desc',
		loadMsg:LOADING_MESSAGE,
		border:false,
		nowrap: false,
		striped: true,
		toolbar:TOOLBAR,
		remoteSort:false,
		rownumbers:true,
		pagination:PAGE,
		collapsible:true,
		singleSelect:SINGLE,
		columns:[COLUMNS],
		onSelect: function(rowIndex, rowData){
			//alert(rowData);
			//如果状态等于空 或者状态不等于未启动
			if(rowData.workflowStatus != undefined){
				
				if(rowData.workflowStatus == '未启动'){
					$('#edit' + BUSINESSNAME + 'Btn').linkbutton('enable');
					$('#del' + BUSINESSNAME + 'Btn').linkbutton('enable');
				}else{
					$('#edit' + BUSINESSNAME + 'Btn').linkbutton('disable');
					$('#del' + BUSINESSNAME + 'Btn').linkbutton('disable');
				}
				
				/*if(rowData.workflowStatus != '' && rowData.workflowStatus != '未启动' && rowData.workflowStatus == '已完成'){
					$('#edit' + BUSINESSNAME + 'Btn').linkbutton('disable');
					$('#del' + BUSINESSNAME + 'Btn').linkbutton('disable');
				}else{
					
				}*/
			}
		}
	});
}

/**
 * 初始化数据表格分页模块
 * @param DATAGRID_ID
 * @param URL
 * @param QUERYS
 */
function DG_PAGE_INIT(DATAGRID_ID, URL, QUERYS){
	var pager = DATAGRID_ID.datagrid('getPager');
	$(pager).pagination({
		pageSize: 15,
		pageList: [15, 30, 50, 100],
		beforePageText: '第',
		afterPageText: '页    共 {pages} 页',
		displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
		onSelectPage:function(pageNumber, pageSize){
			DATAGRID_ID.datagrid('options').url = URL + '?pageNo=' + pageNumber + '&pageSize='+ pageSize;
			if(QUERYS != null){
				DATAGRID_ID.datagrid('reload', QUERYS);				
			}else{
				DATAGRID_ID.datagrid('reload');				
			}
		}
	});
}

/**
 * 取得数据表格选中行
 * @param DATAGRID_ID	业务数据表格ID
 * @returns
 */
function DG_GETROW(DATAGRID_ID){
	return DATAGRID_ID.datagrid('getSelected');
}

/**
 * 重新加载表格数据
 * @param DATAGRID_ID	业务数据表格ID
 */
function DG_RELOAD(DATAGRID_ID){
	DATAGRID_ID.datagrid('reload');
}

/**
 * 根据条件重新加载数据
 * @param DATAGRID_ID	业务数据表格ID
 * @param URL			业务数据请求地址
 * @param QUERYS		查询条件集合
 */
function DG_QUERY(DATAGRID_ID, URL, QUERYS){
	DATAGRID_ID.datagrid('options').url = URL;
	DATAGRID_ID.datagrid('reload', QUERYS); 
}

/**
 * 根据条件重新加载分页数据
 * @param DATAGRID_ID
 * @param URL
 * @param QUERYS
 */
function DG_QUERY_PAGE(DATAGRID_ID, URL, QUERYS){
	DATAGRID_ID.datagrid('options').url = URL;
	DATAGRID_ID.datagrid("options").pageNumber = 1;   
	DATAGRID_ID.datagrid('reload', QUERYS); 
	DATAGRID_ID.datagrid('getPager').pagination({pageNumber: 1}); 
}